//
//  FSMessageViewController.m
//  WZSOA
//
//  Created by 刘沿 on 16/7/1.
//  Copyright © 2016年 刘沿. All rights reserved.
//

#import "FSMessageViewController.h"
#import "FSMessageCell.h"
#import "FSMessageDetailController.h"

@interface FSMessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableVIew;

@property(nonatomic,assign)int currentPage; //当前页

@property(nonatomic,assign)NSInteger totalCounts; //总条数

@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation FSMessageViewController

static NSString *const reuserID = @"Message";


-(NSMutableArray *)dataArray{
 
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor colorWithRed:188/255.0 green:193/255.0 blue:196/255.0 alpha:1];

    UITableView *tab = [[UITableView alloc] init];
    
    tab.delegate = self;
    
    tab.dataSource = self;
    
    self.tableVIew = tab;
    
    [self.view addSubview:tab];
    
    [tab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).mas_offset(50);
        
        make.left.equalTo(self.view.mas_left).mas_offset(20);
        
        make.right.equalTo(self.view.mas_right).mas_offset(-20);
        
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-20);
    }];
    
    __weak typeof (self) weakSelf = self;
    
    [self.tableVIew addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableVIew headerBeginRefreshing];
    
    [self.tableVIew addFooterWithCallback:^{
        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
            
            [weakSelf loadMoreData];
            
        }else{
            
            [weakSelf.tableVIew footerEndRefreshing];
            [weakSelf.view makeToast:@"没有更多了"];
            
        }
        
    }];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"返回"
                                   style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backButton];
    
//创建右侧新建按钮
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"新建" style:UIBarButtonItemStylePlain target:self action:@selector(newMES)];
    
    self.navigationItem.rightBarButtonItem = rightItem;

 
}

//新建短信
- (void)newMES{

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    FSNewMessageController *newM = [sb instantiateViewControllerWithIdentifier:@"NewMessageVC"];
    
    [self.navigationController pushViewController:newM animated:YES];

}

- (void)loadData{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    
    NSArray * array = @[@{@"userId":userId},@{@"page":[NSString stringWithFormat:@"%ld",(long)self.currentPage]}];

    [HttpTools requestWithURL:Kexchother arrays:array method:KmessagePage success:^(id JSON) {
            
        NSArray *tempArray = JSON[@"data"];
        
        for (NSDictionary *dict in tempArray) {
            
            FSMessageModel *model = [FSMessageModel mj_objectWithKeyValues:dict];
            
            [self.dataArray addObject:model];
        }
        
        self.totalCounts = [JSON[@"number"] integerValue];
        
        self.currentPage ++;
        
        [self.tableVIew reloadData];
        
        [self.tableVIew headerEndRefreshing];
        
        } failure:^(NSError *error) {
        
        }];
    
}

- (void)loadMoreData{

    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    
    NSArray * array = @[@{@"userId":userId},@{@"page":[NSString stringWithFormat:@"%ld",(long)self.currentPage]}];
    
    [HttpTools requestWithURL:Kexchother arrays:array method:KmessagePage success:^(id JSON) {
        
        NSArray *tempArray = JSON[@"data"];
        
        for (NSDictionary *dict in tempArray) {
            
            FSMessageModel *model = [FSMessageModel mj_objectWithKeyValues:dict];
            
            [self.dataArray addObject:model];
        }
        
        self.totalCounts = [JSON[@"number"] integerValue];
        
        [self.tableVIew reloadData];
        
        self.currentPage ++;
        
        [self.tableVIew footerEndRefreshing];
        
    } failure:^(NSError *error) {
        
    }];


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    FSMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Message"];

    if (cell == nil) {
        
        cell = [FSMessageCell MessageCell];
    }
    
    FSMessageModel *model = self.dataArray[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    CGFloat H = 44;

    FSMessageModel *model = self.dataArray[indexPath.row];
    
    CGFloat titleSize = [FSHightTool heightbyString:model.acceptphoneuser font:15 minsWidth:50];
    
    CGFloat desSize = [FSHightTool heightbyString:model.subject font:15 minsWidth:50];
    
    
    H = titleSize + desSize + 40;

    return H;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    FSMessageModel *model = self.dataArray[indexPath.row];
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    FSMessageDetailController *messageVC = [sb instantiateViewControllerWithIdentifier:@"MessageDetailVC"];
    
    messageVC.model = model;
    
    [self.navigationController pushViewController:messageVC animated:YES];

}



@end




















